package com.atguigu.test.mvc.controller;

import com.atguigu.test.mvc.annotation.PrivacyEncrypt;
import com.atguigu.test.mvc.annotation.Token;
import com.atguigu.test.mvc.annotation.Yaml;
import com.atguigu.test.mvc.bean.People;
import com.atguigu.test.po.DemoObj;
import com.atguigu.test.po.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;

/**
 * @author zp
 * @version 1.0
 * @description: 自定义处理器
 * @date 2022/12/31 16:30
 */
@Controller
@Slf4j
public class MyController {

    @RequestMapping("/exception")
    public ModelAndView getException(){
        int i=10/0;
        return null;
    }



    @GetMapping("/myData")
    @ResponseBody
    public String myData(@RequestParam("body") String body, Date date,ModelAndView modelAndView) {
        log.info("myData方法被调用");
//        User user = new User();
//        user.setUserName("李四");
        modelAndView.getModel().get("b");

        modelAndView.getModel().get("date");
        return body+"全局属性添加的参数 b:[ "+modelAndView.getModel().get("b")+"时间数据date："+date+" ]";
    }
    /**
     * @description: 测试自定义 httpMessage
     * @author zp
     * @date: 2023/1/16 20:24
     * @return String
     */
    @RequestMapping(value = "/testHttpMessage", produces = { "application/x-eleven" })
    public @ResponseBody DemoObj testHttpMessage(@RequestBody DemoObj demoObj) {
        return demoObj;
    }
    @GetMapping("/test1")
    public ModelAndView test1() {
        log.info("[test1()]");
        return null;
    }

    @PostMapping("/test2")
    public ModelAndView test2(@RequestParam("name") String name) {
        log.info("test2([{}])", name);
        return null;
    }

    @PutMapping("/test3")
    public ModelAndView test3(@Token String token) {
        log.info("[test3({})]", token);
        return null;
    }

    @GetMapping("/test4")
    @Yaml
    public User test4() {
        log.info("test4()");
        User user = new User();
        user.setUserName("李四");
        return user;
    }

    @RequestMapping("/getPeople")
    @PrivacyEncrypt
    public People getPeople() {
        People people = new People();
        people.setId(1232123);
        people.setName("张三三");
        people.setSex(16);
        people.setAge(1);
        people.setPhone("12345678911");
        people.setEmail("123456@qq.com");
        people.setSign("qwe123456789");
        return people;
    }

    @RequestMapping("/400")
    public String noPage(){
        return "404";
    }
//    /**
//     * @description:  配置了当前的错误响应 配置了springboot支持的错误页面映射 所以注释
//     * @author zp
//     * @date: 2023/1/16 19:18
//     * @param request
//     * @return Map<Object>
//     */
//    @RequestMapping("/error")
//    @ResponseBody
//    public Map<String,Object> errorPage(HttpServletRequest request){
//        Throwable error = (Throwable)request.getAttribute(RequestDispatcher.ERROR_EXCEPTION);
//        Map<String,Object> map = new HashMap<>(8);
//        map.put("error", error.getMessage());
//        return map;
//    }
}
